package com.nogroup.springbootproject.service.impl;

import com.nogroup.springbootproject.dto.StudentDto;
import com.nogroup.springbootproject.entity.Student;
import com.nogroup.springbootproject.dao.StudentDao;
import com.nogroup.springbootproject.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Student)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 18:45:33
 */
@Service("studentService")
@Transactional(rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param studentCode 主键
     * @return 实例对象
     */
    @Override
    public Student queryById(String studentCode) {
        return this.studentDao.queryById(studentCode);
    }

    /**
     * 分页查询
     *
     * @param student 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Student> queryByPage(Student student, PageRequest pageRequest) {
        long total = this.studentDao.count(student);
        return new PageImpl<>(this.studentDao.queryAllByLimit(student, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student insert(Student student) {
        this.studentDao.insert(student);
        return student;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student update(Student student) {
        this.studentDao.update(student);
        return student;
    }

    /**
     * 通过主键删除数据
     *
     * @param student 主键
     * @return 是否成功
     */
    @Override
    public Student deleteById(Student student) {
        this.studentDao.deleteById(student);
        return  student;
    }

    /**
     * 根据名字模糊查询学生
     * @param studentDto
     * @return
     */
    @Override
    public List<Student> getStudentByNameList(StudentDto studentDto) {
        return studentDao.getStudentByNameList(studentDto);
    }

    /**
     * 根据模糊查询学生的人数
     * @param studentDto
     * @return
     */
    @Override
    public long countPage(StudentDto studentDto) {
        return studentDao.countPage(studentDto);
    }

    /**
     * 获取所有学生
     * @return
     */
    @Override
    public List<Student> getAllStudents(){
        return studentDao.getAllStudents();
    }

    @Override
    public List<Student> getStudentsByFuzzySearch(StudentDto studentDto) {
        return studentDao.getStudentsByFuzzySearch(studentDto);
    }
}
